package au.com.iglootech.TaxTimeToolTip.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.InvoiceNumber;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.service.InvoiceNumberService;

@Component(InvoiceNumberServiceImpl.BEANNAME)
public class InvoiceNumberServiceImpl implements InvoiceNumberService {
	public static final String BEANNAME = "InvoiceNumberServiceImpl";
	@Autowired
	private transient GenericDao genericDao;

	@Override
	public Integer getNextNumber() {
		List<InvoiceNumber> numbers = new ArrayList<InvoiceNumber>(genericDao.findAll(InvoiceNumber.class));
		if(numbers == null || numbers.size()<=0)
		{
			// insert the first one
			InvoiceNumber start = new InvoiceNumber();
			start.setInvoiceNumber(100);
			genericDao.save(start);
			return 100;
		}
		else
		{
			InvoiceNumber now = numbers.get(0);
			Integer invoiceNum = now.getInvoiceNumber()+1;
			now.setInvoiceNumber(invoiceNum);
			genericDao.save(now);
			return invoiceNum;
		}
	}

}
